﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace Web.fn.upload
{
    /// <summary>
    /// UploadHandler 的摘要说明
    /// </summary>
    public class UploadHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";

            if (context.Session["user_file_upload"] != null)
            {
                string str_prefix = context.Session["user_file_upload"].ToString();
            }
            if (context.Request.Form["someKey"]!=null)
            {
                string str_keyvalue = context.Request.Form["someKey"];
            }
            try
            {
                HttpPostedFile file = context.Request.Files["Filedata"];
                string uploadPath = HttpRuntime.AppDomainAppPath + "\\uploadfiles\\";
                // HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";

                if (file != null)
                {
                    if (!Directory.Exists(uploadPath))
                    {
                        Directory.CreateDirectory(uploadPath);
                    }
                    file.SaveAs(uploadPath + file.FileName);
                    //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                    context.Response.Write("1");
                }
                else
                {
                    context.Response.Write("0");
                }
            }
            catch (Exception ex)
            {
                context.Response.Write("0");
                throw ex;
                //throw new  HttpException(405,"chaochang") ;
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}